<?php

	require('../database/bd_connect.php');
		
	if (isset($_GET['opt'])){	
		$cpe = $_GET['opt'] ;
	
		if (isset($_GET['query'])){	
						$query = $_GET['query'] ;
				$vodafone_start = 0;
				$pt_start = 0 ;
				$nos_start = 0 ;
				$outros_start = 0 ;

				$cpe_ssid = $_GET['cpe_ssid'];
				
				if (strpos($cpe_ssid, "Vodafone") === 0) {
					$vodafone_start=1;
				}
				elseif (strpos($cpe_ssid, "MEO") === 0) {
					$pt_start=1;
				}
				elseif ((strpos($cpe_ssid, "NOS") === 0) || (strpos($cpe_ssid, "ZON-") === 0)){
					$nos_start=1;
				}
				else  {
					$outros_start=1;
				}
	//DEFAULT
	//$sql = "SELECT * FROM wifi_data WHERE cpe_id=".$cpe." LIMIT 100";
	//ULTIMOS 10 MINUTOS
	if($query==1){
	$sql = "SELECT DISTINCT ssid,mac, DATE_FORMAT(time, '%H:%i') as time FROM wifi_data WHERE ssid NOT LIKE 'FON_ZON_FREE_INTERNET' AND ssid NOT LIKE 'MEO-WIFI%' AND cpe_id=".$cpe." AND time BETWEEN DATE_SUB(NOW(), INTERVAL 10 MINUTE) AND DATE_ADD(NOW(), INTERVAL 0 MINUTE) group by mac, minute(time) order by minute(time),ssid ASC;";
	}
	else if($query==2){
	//ULTIMOS 16 HORAS INTERVALOS DE 2 HORAS
	//	$sql = "SELECT * FROM wifi_hour WHERE cpe_id=".$cpe." AND time >= NOW() - INTERVAL 16 HOUR AND mod(hour(time),2)";
	$sql = "SELECT DISTINCT ssid,mac, DATE_FORMAT(time, '%M %d') as time FROM wifi_hour WHERE ssid NOT LIKE 'FON_ZON_FREE_INTERNET' AND ssid NOT LIKE 'MEO-WIFI%' AND cpe_id=".$cpe." AND time >= DATE_SUB(CURDATE(), INTERVAL 6 DAY) group by mac, day(time) order by date(time),ssid ASC";
	}
	else if($query==3){
	//ULTIMOS 7 DIAS INTERVALOS DE 12 HORAS
	//$sql = "SELECT * FROM wifi_hour WHERE cpe_id=".$cpe." AND time >= NOW() - INTERVAL 7 DAY AND (hour(time)=0 OR hour(time)=12) ";
	$sql = "SELECT DISTINCT ssid, DATE_FORMAT(time, 'Week %U in %M') as time  FROM wifi_hour WHERE ssid NOT LIKE 'FON_ZON_FREE_INTERNET' AND ssid NOT LIKE '%MEO-WIFI%' AND cpe_id=".$cpe." AND time >= NOW() - INTERVAL 3 week group by week(time),mac order by date(time)";
	}
	
	$result = $conn->query($sql);
	$num_rows = mysqli_num_rows($result);
	$vodafone = $vodafone_start;
	$pt = $pt_start ;
	$nos = $nos_start;
	$outros = $outros_start;

	$instance = -1 ;
	$firstrun = 0 ;

	while ($row = mysqli_fetch_array($result)) {
		
		//echo $row['time'] . "||" . $time   ;
			$time=$row['time'];
			$ssid=$row['ssid'];
			if($firstrun == 0){
				$firstrun = 1 ;
				$instance = $time;
				
			}
			if($instance != $time){
				
				$timeseries[] = $instance;
				$instance = $time ;
				$vodafoneseries[] = $vodafone;
				$vodafone = $vodafone_start;
				$ptseries[] = $pt;
				$pt = $pt_start ;
				$nosseries[] = $nos;
				$nos = $nos_start;
				$outrosseries[] = $outros;
				$outros = $outros_start;
				//echo "tempo mudou... reseting <br \>" ;

				if (stripos($ssid, "Vodafone") === 0) {
					$vodafone++;
				}
				elseif (stripos($ssid, "MEO") === 0) {
					$pt++;
				}
				elseif ((stripos($ssid, "NOS") === 0) || (stripos($ssid, "ZON-") === 0)){
					$nos++;
				}
				else  {
					$outros++;
				}		
			}
			else {
				
				if (stripos($ssid, "Vodafone") === 0) {
					$vodafone++;
					
				}
				elseif (stripos($ssid, "MEO") === 0) {
					$pt++;
				}
				elseif ((stripos($ssid, "NOS") === 0) || (stripos($ssid, "ZON-") === 0)){
					$nos++;
				}
				else  {
					$outros++;
				}
			}
			$i++;
			//echo ($num_rows);
			if($i==$num_rows){
				$vodafoneseries[] = $vodafone;
				$ptseries[] = $pt;
				$nosseries[] = $nos;
				$outrosseries[] = $outros;
				$timeseries[] = $time ;
			}		
	}

function calculate_average($arr) {
    $count = count($arr); //total numbers in array
    foreach ($arr as $value) {
        $total = $total + $value; // total value of array numbers
    }
    $average = ($total/$count); // get average value
    return $average;
}
	if ( $num_rows != FALSE ){
	$vodafone = calculate_average($vodafoneseries);
	$pt = calculate_average($ptseries);
	$nos = calculate_average($nosseries);
	$outros = calculate_average($outrosseries);
}
	
		if ( $num_rows == FALSE ){
			if($query==1){
				$sql = "SELECT DISTINCT DATE_FORMAT(time, '%H:%i') as time FROM noise_data WHERE cpe_id=".$cpe." AND time BETWEEN DATE_SUB(NOW(), INTERVAL 10 MINUTE) AND DATE_ADD(NOW(), INTERVAL 0 MINUTE) ;";
			}
			else if($query==2){
			//ULTIMOS 16 HORAS INTERVALOS DE 2 HORAS
			//	$sql = "SELECT * FROM wifi_hour WHERE cpe_id=".$cpe." AND time >= NOW() - INTERVAL 16 HOUR AND mod(hour(time),2)";
			//$sql = "SELECT ssid, DATE_FORMAT(time, '%M %d') as time FROM wifi_hour WHERE ssid NOT LIKE 'FON_ZON_FREE_INTERNET' AND ssid NOT LIKE 'MEO-WIFI%' AND cpe_id=".$cpe." AND time >= NOW() - INTERVAL 6 DAY group by mac, day(time) order by date(time)";
				//$sql = "SELECT DISTINCT ssid,mac, DATE_FORMAT(time, '%M %d') as time FROM wifi_hour WHERE ssid NOT LIKE 'FON_ZON_FREE_INTERNET' AND ssid NOT LIKE 'MEO-WIFI%' AND cpe_id=".$cpe." AND time > NOW() - INTERVAL 7 DAY group by mac, day(time) order by date(time)";
				$sql = "SELECT DISTINCT DATE_FORMAT(time, '%M %d') as time FROM noise_hour WHERE cpe_id=".$cpe." AND time >= DATE_SUB(CURDATE(), INTERVAL 6 DAY) group by day(time)";
			}
			else if($query==3){
			//ULTIMOS 7 DIAS INTERVALOS DE 12 HORAS
			//$sql = "SELECT * FROM wifi_hour WHERE cpe_id=".$cpe." AND time >= NOW() - INTERVAL 7 DAY AND (hour(time)=0 OR hour(time)=12) ";
				$sql = "SELECT DISTINCT DATE_FORMAT(time, 'Week %U in %M') as time  FROM noise_hour WHERE cpe_id=".$cpe." AND time >= NOW() - INTERVAL 3 week group by week(time) ";
			}
			$result = $conn->query($sql);
			while ($row = mysqli_fetch_array($result)) {
				if (strpos($cpe_ssid, "Vodafone") === 0) {
					$vodafone = 1;
				}
				elseif (strpos($cpe_ssid, "MEO") === 0) {
					$pt = 1;
				}
				elseif ((strpos($cpe_ssid, "NOS") === 0) || (strpos($cpe_ssid, "ZON-") === 0)){
					$nos = 1;
				}
				else  {
					$outros = 1;
				}
				$timeseries[] = $row['time'] ;
			}
	}


	$output = array($vodafone,$pt,$nos,$outros);

	echo json_encode($output);


	//echo ("<br \>vodafone: " . $vodafone . " pt: " . $pt. " nos: " . $nos . " outros: " . $outros ."<br \>") ;
	//var_dump($timeseries);
	//echo(json_encode($timeseries)) ;
	//echo "<br \>---fim" ;

}
}
else {
	echo "erro";
}
mysqli_close($conn);
?>
